package com.yunfei.schedule;

import com.ylcaifu.common.auto.util.StringUtil;
import com.yunfei.bo.TblobstorageBo;
import com.yunfei.entity.KfCrmProductAppendixInfoEntity;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.crm.TblobtSorageMapper;
import com.yunfei.mapper.ods.KfCrmProjectAppendixInfoMapper;
import com.yunfei.utils.ComponentFactory;
import com.yunfei.utils.DownloadPdf;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;

@Component
public class ScheduledTask {
    @Value("${file.ip.address}")
    String ipAddress;
    @Value("${file.savePath}")
    String savePath;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTask.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

 /*  @Scheduled(cron = "0 0 1 * * ?")  */ //每天凌晨1点执行
   @Scheduled(cron = "0 0/1 * * * ?")   //每1分钟
   /*@Scheduled(fixedRate = 50000000)*/
    public void testMyBatis() {
        System.out.println("定时从crm文件服务器获取所有附件信息");
        int newCount=0;
        int oldCount=0;
        try {

            KfCrmProjectAppendixInfoMapper kfCrmProjectAppendixInfoMapper = ComponentFactory.getBean(KfCrmProjectAppendixInfoMapper.class);
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            TblobstorageBo queryParam=new TblobstorageBo();
            List<TblobstorageEntity> list=tblobtSorageMapper.findAllList(queryParam);//crm所有附件信息
            //客服app所有附件信息
            int size=0;
            if(!StringUtil.isNullOrEmpty(list)){
                size=list.size();
            }
            if(size>0){
                System.out.println("总条数========================"+size);
                for(int i=0;i<size;i++){
                    TblobstorageEntity tblobstorageBo=list.get(i);
                    KfCrmProductAppendixInfoEntity kfCrmProductAppendixInfoBo=new KfCrmProductAppendixInfoEntity();
                    String cFilePath=tblobstorageBo.getcFilepath();
                    //获取原始文件存放地址
                    String oFileUrl=ipAddress+"/"+cFilePath.replaceAll("\\\\","/");
                   // System.out.println("oFileUrl===================="+oFileUrl);
                    //设置主键
                    String pkId=tblobstorageBo.getlStorageid() ;
                    String productId=tblobstorageBo.getcSubkeyid();
                    String oldName=tblobstorageBo.getcFilename();
                    String prix=".pdf";
                    if(!StringUtil.isNullOrEmpty(oldName)&&oldName.contains(".")){
                        prix=oldName.substring(oldName.lastIndexOf("."),oldName.length());
                    }
                    String fileName=pkId+prix;
                    String uploadAddress="/photo/"+fileName;
                    String localFilePath=savePath;
                        String operator="05783596-a269-475c-9878-a3930475efc3";//系统管理员ID
                        String visible="01";
                        String trueAddress=localFilePath+fileName;
                        String remark1=tblobstorageBo.getcCatalog();//存放附件表里的catalog
                        String remark2=productId;//remark2存放产品ID
                        String remark3=tblobstorageBo.getcFilepath();//remark3存放附件在crm里面的存放路径
                        kfCrmProductAppendixInfoBo.setPkId(pkId);
                        System.out.println("产品id========================="+productId);
                        kfCrmProductAppendixInfoBo.setProductId(productId);
                        kfCrmProductAppendixInfoBo.setFileName(fileName);
                        kfCrmProductAppendixInfoBo.setUploadAddress(uploadAddress);
                        kfCrmProductAppendixInfoBo.setOperator(operator);
                        kfCrmProductAppendixInfoBo.setVisible(visible);
                        kfCrmProductAppendixInfoBo.setOldName(oldName);
                        kfCrmProductAppendixInfoBo.setRemark1(remark1);
                        kfCrmProductAppendixInfoBo.setRemark2(remark2);
                        kfCrmProductAppendixInfoBo.setRemark3(remark3);
                        String dCreateDate=tblobstorageBo.getdCreatedate();//获取crm附件上传时间
                         kfCrmProductAppendixInfoBo.setCreateDate(dCreateDate);
                        kfCrmProductAppendixInfoBo.setTrueAddress(trueAddress);
                        //判断文件是否已经存在
                        KfCrmProductAppendixInfoEntity query=new KfCrmProductAppendixInfoEntity();
                        query.setPkId(pkId);
                        int count=kfCrmProjectAppendixInfoMapper.count(query);
                        if(count==0){
                            boolean flag=DownloadPdf.downLoadServerFileToLocal(oFileUrl,fileName,localFilePath);
                            if(flag){
                                kfCrmProjectAppendixInfoMapper.add(kfCrmProductAppendixInfoBo);
                            }
                            newCount++;
                        }else{
                            oldCount++;
                        }


                }

            }

            if (list != null) {

                System.out.println(list.size()+"条任务处理完成,其中新增 "+newCount+"条,已存在 "+oldCount+"条");

            } else {
                System.out.println("我什么事都不用做，等待下次再说吧");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

